﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Resto.CostControl.Core;
using QuickApp.Core;
using Resto.CostControl.Core.DWZAdapter;
using EfSearchModel.Model;
using QuickApp.MVCControl.FlexiGrid;

namespace Resto.CostControl.Web.Controllers
{
    public class DicController : Controller
    {
        private readonly IRepository<DicItem> _RepoDicItem;
        private readonly IUnitOfWork _UnitOfWork;

        public DicController(IRepository<DicItem> repo, UnitOfWork unitOfWork)
        {
            _RepoDicItem = repo;
            _UnitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult List(DWZPageInfo pageInfo, QueryModel where)
        {
            //where.Items.Add(new ConditionItem("TypeCode", QueryMethod.Equal, "DishType"));
            var list = _RepoDicItem.GetPageable(pageInfo, where);

            var plist = new PagedList<DicItem> { DataList = list.Page.ToList(), Total = list.Total, PageIndex = pageInfo.PageIndex, PageSize = pageInfo.PageSize };
            JsonFlexiGridData fdata = JsonFlexiGridData.ConvertFromPagedList<DicItem>(plist, pageInfo.ColKey, pageInfo.ColsInfo);
            return Json(fdata);
        }

        public ActionResult Edit(string Id)
        {
            DicItem data = null;
            if (!string.IsNullOrWhiteSpace(Id))
            {
                data = _RepoDicItem.Find(Id);
            }
            else
            {
                data = new DicItem();
            }

            return View(data);
        }

        [HttpPost]
        public JsonResult Save(DicItem data)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(data.Id))
                {
                    data.Id = Guid.NewGuid().ToString();
                    data.UpdateDate = DateTime.Now;
                    _RepoDicItem.Insert(data);
                }
                else
                {
                    var oldData = _RepoDicItem.Find(data.Id);
                    data.UpdateDate = DateTime.Now;
                    TryUpdateModel(oldData);
                }

                _UnitOfWork.Commit();
            }

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dic",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }

        [HttpPost]
        public JsonResult Delete(string id)
        {
            _RepoDicItem.Delete(id);
            _UnitOfWork.Commit();

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dic",
                callbackType = "",
                forwardUrl = "",
            };
            return Json(result);
        }
    }
}
